<link href="<?php echo base_url('theme/css/frontend/search.css'); ?>" rel="stylesheet">
<script src="<?php echo base_url('theme/third_party/rating/lib/jquery.raty.min.js'); ?>" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $.fn.raty.defaults.path = '<?php echo base_url('theme/third_party/rating/lib/img'); ?>';
        $('#create-recruiment').click(function() {
            var user_id = $('input[name=user_id]').val();
            if (user_id == 0) {
                alert("Bạn cần phải đăng nhập để tạo hồ sơ!")
            } else {
                window.location = 'recruitment/add';
            }
        });
        // Hiển thị ô kết quả rating
        $('.rate-view').each(function() {
            $(this).raty({
                number: 5,
                score: $(this).attr('tag'),
                readOnly: true,
                half: true,
                size: 16,
                starHalf: 'small_star-half.png',
                starOff: 'small_star-off.png',
                starOn: 'small_star-on.png'
            });
        });
    });
    function loadComment(recruitment_id) {
        $(".popup-comment").each(function() {
            if ($(this).attr('tag') == recruitment_id) {
                if ($(this).css("display") !== "none") {
                    $(this).hide();
                } else {
                    $(this).show();
                    $('.comment-content[tag=' + recruitment_id + ']').val('');
                }
            } else {
                $(this).hide();
            }
        });
    }
    function sendComment(user_id, recruitment_id) {
        var key = window.event.keyCode;
        isShift = window.event.shiftKey ? true : false;
        // If the user has pressed only enter
        if (key == 13 && !isShift) {
            $('.comment-content').each(function() {
                if ($(this).attr('tag') == recruitment_id) {
                    var comment = $(this).val();
                    $.ajax({
                        url: '/recruitment/comment/' + user_id + '/' + recruitment_id + '/' + comment,
                        type: "POST",
                        success: function(data) {
                            alert("Bình luận thành công!");
                            count = $(".comment[tag = " + recruitment_id + "]").text();
                            new_count = parseInt(count) + 1;
                            if(new_count < 10) {
                                new_count = '0' + new_count;
                            }
                            $(".comment[tag = " + recruitment_id + "]").text(new_count);
                            $(".popup-comment[tag = " + recruitment_id + "]").hide();
                        }
                    });
                }
            });
        }
    }
</script>
<?php if ($recruitments): ?>
    <?php if (!isset($extra)): ?>
        <ul class="thumbnails items">
        <?php endif; ?>
        <?php $i = 0; ?>
        <?php foreach ($recruitments as $recruitment): ?>
            <?php $i++; ?>
            <li style="height: 125px;line-height: 15px;font-size: 11px;">
                <a style="display:block;" rel="recruitmentPopup" href="<?php echo base_url('recruitment/view/' . $recruitment['recruitment_id'] . '/true'); ?>" data-url="<?php echo '/tuyen-dung/' . $recruitment['recruitment_id']; ?>" >
                    <div class="row">
                        <div class="pull-left">
                            <div style="display:block; height:56px; width:50px; position:relative;padding-top: 6px;">
                                <img style="position:absolute;top:0;left:0;bottom:0;right:0;margin:auto;" src="<?php echo getAvatarLink($recruitment['user_id'], $recruitment['avatar']); ?>">
                            </div>
                        </div>
                        <div class="pull-left" style="margin-left: 10px; width: 164px;">
                            <b style="font-size: 12px; color: #1C5AC4;"><?php echo $recruitment['real_name']; ?></b> <br />
                            Giới tính: <?php echo getSexType($recruitment['sex']); ?> <br />
                            Sinh ngày: <?php echo date('d/m/Y', strtotime($recruitment['birthday'])); ?> <br />
                            Nơi làm việc: <?php echo getCity($recruitment['work_place']); ?> <br />
                        </div>
                    </div>
                    <div class="row">
                        <div><b style="font-size: 12px; color: #F8A324;"><?php echo getCareerType($recruitment['career']); ?></b></div>
                        <div>
                            Kinh nghiệm: <?php echo getYearsOfExperienceType($recruitment['years_of_experience']); ?><br />
                            Mức lương: <?php echo $recruitment['salary']; ?>
                        </div>
                    </div>
                </a>
                <div class="caption-left" style="margin-top:5px;">
                    <div class="rate-view" tag=""></div>
                </div>
                <div class="caption-right" style="margin-top:6px!important;">
                    <div class="comment" onclick="loadComment('<?php echo $recruitment['recruitment_id']; ?>');" tag="<?php echo $recruitment['recruitment_id']; ?>">
                        <?php echo $recruitment['count_comment']; ?>
                    </div>
                    <div class="detail" onclick="loadDetail('');">
                    </div>
                </div>
                <?php if ($i % 4 == 0) { ?>
                    <div class="popup-comment" tag="<?php echo $recruitment['recruitment_id']; ?>" style="margin-top: 12px;margin-left: -236px;">
                    <?php } else { ?>
                        <div class="popup-comment" tag="<?php echo $recruitment['recruitment_id']; ?>" style="margin-top: 12px;margin-left: -1px;">
                        <?php } ?>
                        <div class="header">
                            <div class="name">
                                <b><?php echo $user['real_name']; ?></b> đang bình luận điều này
                            </div>
                            <div class="date">
                                <?php
                                date_default_timezone_set('Asia/Ho_Chi_Minh');
                                echo date("h:i - d/m/Y");
                                ?>
                            </div>
                        </div>
                        <div class="content">
                            <div class="avatar">
                                <?php if (isset($user['avatar'])) { ?>    
                                    <img src="<?php echo getAvatarLink($user['user_id'], $user['avatar']); ?>">
                                <?php } ?>
                            </div>
                            <div class="input" style="padding-bottom: 6px;">
                                <textarea style="width:400px;" onkeydown="sendComment('<?php echo $user['user_id']; ?>', '<?php echo $recruitment['recruitment_id']; ?>')" class="comment-content" tag="<?php echo $recruitment['recruitment_id']; ?>" placeholder="Viết bình luận"></textarea>
                            </div>
                        </div>
                    </div>
            </li>
        <?php endforeach; ?>

        <?php if (!isset($extra)): ?>
        </ul>
        <script type="text/javascript">
            // Thanh cuộn load thêm nội dung
            $(document).ready(function() {

                // js.js
                recruitmentPopup();

                var company_extra = 0;
                window.onscroll = function() {
                    scrollY = window.pageYOffset;
                    scrollMaxY = document.documentElement.scrollHeight - document.documentElement.clientHeight;
                    if (scrollY == scrollMaxY) {
                        company_extra++; //s tăng lên một mỗi lần cuộn
                        var url = '<?php echo base_url('recruitment/index'); ?>';
                        var url = url + '\/' + company_extra + '/true';

                        $.get(url, function(data) {
                            $('.items').append(data); // append to <ul>

                            // js.js
                            recruitmentPopup();

                        });
                    }
                }; // end onscroll

            });
        </script> 
    <?php endif; ?>
<?php else: ?>
    Không tồn tại tuyển dụng nào.
<?php
endif; // $recruitments ?>